/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import dao.LoaiSanPhamDAO;
import dao.SanPhamDAO;
import java.awt.Component;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JOptionPane;
import pojo.LoaiSanPham;
import pojo.SanPham;

/**
 *
 * @author hieupham
 */
public class DanhSachSanPhamGui extends javax.swing.JPanel {

    /**
     * Creates new form DanhSachSanPhamGui
     */   
    private JErrorProvider errSanPham;
     private List<LoaiSanPham> loaisppojo;
     private List<SanPham> sanphams;
    public DanhSachSanPhamGui() throws Exception {
        initComponents();
        SetTabOrder();
        Autoload();
    }
    
    private void SetTabOrder(){
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy=new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{cmbMaLoaiSP,btnThemLoaiSP,txtTenLoaiSP,txtDonGia,txtMaSP,txtTenSP,btnTimSP,btnNhapSP,btnSua, btnXoaSP,btnClear});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel9 = new javax.swing.JLabel();
        jFileChooser1 = new javax.swing.JFileChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSachSP = new org.jdesktop.swingx.JXTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbDanhSachSanPham = new org.jdesktop.swingx.JXTable();
        jPanel5 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        btnTimSP = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtMaSP_Tim = new javax.swing.JTextField();
        txtTenSP_Tim = new javax.swing.JTextField();
        txtMaLSP_Tim = new javax.swing.JTextField();
        txtTenLSP_Tim = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtDonGiaTu = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtDonGiaDen = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        cmbMaLoaiSP = new javax.swing.JComboBox();
        btnThemLoaiSP = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        txtTenLoaiSP = new javax.swing.JTextField();
        txtDonGia = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtTenSP = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtMaSP = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btnNhapSP = new javax.swing.JButton();
        btnXoaSP = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();

        jLabel9.setText("jLabel9");

        tbDanhSachSP.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhSachSP.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        tbDanhSachSP.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachSPMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDanhSachSP);

        tbDanhSachSanPham.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhSachSanPham.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        tbDanhSachSanPham.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachSanPhamMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbDanhSachSanPham);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông Tin Tìm Kiếm", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 14), new java.awt.Color(0, 0, 0))); // NOI18N

        btnTimSP.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnTimSP.setText("Tìm");
        btnTimSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimSPActionPerformed(evt);
            }
        });

        jLabel1.setText("Mã Sản Phẩm");

        jLabel2.setText("Tên Sản Phẩm");

        jLabel6.setText("Mã Loại Sản Phẩm");

        jLabel8.setText("Tên Loại Sản Phẩm");

        jLabel11.setText("Đơn giá từ");

        jLabel12.setText("Đơn giá đến");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel11)
                            .addGap(58, 58, 58)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(39, 39, 39)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtMaLSP_Tim, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                            .addComponent(txtDonGiaTu))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel12)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtMaSP_Tim, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtTenSP_Tim)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtDonGiaDen)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtTenLSP_Tim, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                        .addGap(14, 14, 14))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnTimSP)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtMaSP_Tim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtTenSP_Tim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtMaLSP_Tim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(txtTenLSP_Tim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel11)
                        .addComponent(txtDonGiaTu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel12))
                    .addComponent(txtDonGiaDen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnTimSP))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Cập Nhật "));

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel4.setText("Mã loại SP");

        cmbMaLoaiSP.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        cmbMaLoaiSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbMaLoaiSPActionPerformed(evt);
            }
        });

        btnThemLoaiSP.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/TN_psd0716.jpg"))); // NOI18N
        btnThemLoaiSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemLoaiSPActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel10.setText("Tên Loại SP");

        txtTenLoaiSP.setEditable(false);

        txtDonGia.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel7.setText("Đơn Giá");

        txtTenSP.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel5.setText("Tên SP");

        txtMaSP.setEditable(false);
        txtMaSP.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel3.setText("Mã SP");

        btnNhapSP.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnNhapSP.setText("Thêm");
        btnNhapSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNhapSPActionPerformed(evt);
            }
        });

        btnXoaSP.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnXoaSP.setText("Xóa ");
        btnXoaSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaSPActionPerformed(evt);
            }
        });

        btnSua.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnClear.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnNhapSP)
                        .addGap(48, 48, 48)
                        .addComponent(btnXoaSP)
                        .addGap(18, 18, 18)
                        .addGap(29, 29, 29)
                        .addComponent(btnSua)
                        .addGap(34, 34, 34)
                        .addComponent(btnClear)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtMaSP, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(cmbMaLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnThemLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel5))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(txtTenSP, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(txtTenLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnThemLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(txtTenLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cmbMaLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(13, 13, 13)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtMaSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtTenSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNhapSP)
                    .addComponent(btnXoaSP)
                    .addComponent(btnSua)
                    .addComponent(btnClear))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(77, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
private void Autoload()throws Exception{
       
        sanphams =(List<SanPham>)SanPhamDAO.LayDanhSach(SanPham.class,MainGUI.nguoiDung);   
        Utility.CreateColumnsFromDB(SanPham.class, sanphams, tbDanhSachSanPham, new String[]{"Mã Sản Phẩm","Tên Sản Phẩm","Ten Loại Sản Phẩm","Đơn Giá","Hình Ảnh"},null);             
        loaisppojo =(List<LoaiSanPham>)LoaiSanPhamDAO.LayDanhSach(LoaiSanPham.class, MainGUI.nguoiDung);        
        Utility.SetDataSourceInCombo(loaisppojo, cmbMaLoaiSP);
        cmbMaLoaiSP.setRenderer(new DefaultListCellRenderer(){
           @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof LoaiSanPham){
                    LoaiSanPham loaisp=(LoaiSanPham)value;
                    setText(String.valueOf(loaisp.getId()));                    
                }               
                return this;
            }            
        });
        cmbMaLoaiSP.setSelectedIndex(-1);      
}
    private void btnNhapSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNhapSPActionPerformed
        try{                
            if(KiemTraTruocKhiThem()>0)
                return ;                                      
            LoaiSanPham lsp =(LoaiSanPham)cmbMaLoaiSP.getModel().getSelectedItem();   
            double gia =Double.valueOf(txtDonGia.getText().trim());
            SanPham sanPham = new SanPham(0,lsp,txtTenSP.getText().trim(),gia,false,null,new HashSet(),new HashSet(),new HashSet());                     
            SanPhamDAO.ThemSanPham(sanPham);   
          //JOptionPane.showMessageDialog(this, "thêm thành công !");
            sanphams.add(sanPham);            
            Utility.CreateColumnsFromDB(SanPham.class, sanphams, tbDanhSachSanPham,new String[]{"Mã San Phẩm","Sản Phẩm", "Tên Loại Sản Phẩm","Đơn Giá","Hình Ảnh"},null);                          
            tbDanhSachSanPham.changeSelection(tbDanhSachSanPham.getModel().getRowCount()-1, 0, true, false);
            JOptionPane.showMessageDialog(this, "thêm thành công !");
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this, "that bai"/*ex.getMessage()*/);
        }
    }//GEN-LAST:event_btnNhapSPActionPerformed

    public int KiemTraTruocKhiThem(){
        if(cmbMaLoaiSP.getModel().getSelectedItem()==null){
             errSanPham.setError(cmbMaLoaiSP, "required");      
            return 1;
        }
        if(txtDonGia.getText().trim().isEmpty()){
             errSanPham.setError(txtDonGia, "required");      
            return 1;
        }
        return 0;
    }
    private void btnThemLoaiSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemLoaiSPActionPerformed
        // TODO add your handling code here:
       QLLoaiSPGui guiLoaiSP = null;
        try {
            guiLoaiSP = new QLLoaiSPGui(null, true);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(DanhSachSanPhamGui.class.getName()).log(Level.SEVERE, null, ex);
        }
       guiLoaiSP.setVisible(true);
    }//GEN-LAST:event_btnThemLoaiSPActionPerformed

    private void cmbMaLoaiSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbMaLoaiSPActionPerformed
         if(cmbMaLoaiSP.getModel().getSelectedItem()!=null)
        {
            LoaiSanPham lsp=(LoaiSanPham)cmbMaLoaiSP.getModel().getSelectedItem();
            txtTenLoaiSP.setText(lsp.getTenLoaiSanPham());
        }
        else
        {
            txtTenLoaiSP.setText("");
        }
    }//GEN-LAST:event_cmbMaLoaiSPActionPerformed

     private void SetDataSanPhamControls(SanPham sanPham)
    {
        cmbMaLoaiSP.getModel().setSelectedItem(sanPham.getLoaiSanPham().getId());
        txtMaSP.setText(String.valueOf(sanPham.getId()));
        txtTenLoaiSP.setText(sanPham.getTenLoaiSanPham());
        txtTenSP.setText(sanPham.getTenSanPham());
        txtDonGia.setText(String.valueOf(sanPham.getDonGia()));       
    }
    private void btnXoaSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaSPActionPerformed
        Object []temp =Utility.GetSelectedRows(tbDanhSachSanPham, sanphams);          
       if(temp.length!=0){           
            if(JOptionPane.showConfirmDialog(this, "hãy chắn rằng muốn xóa cửa hàng này ", "Question", JOptionPane.YES_NO_OPTION)==0){
                try{                                                   
                    int vt =sanphams.indexOf(temp[0]);                          
                    SanPham sanPham =sanphams.get(vt);
                    sanPham.setIsDelete(true);                         
                    SanPhamDAO.XoaSanPham(temp);
               //       JOptionPane.showMessageDialog(this,"xóa thành công");         
                    Utility.RemoveSelectedRows(SanPham.class,tbDanhSachSanPham,sanphams,new String[]{"Mã San Phẩm","Sản Phẩm", "Tên Loại Sản Phẩm","Đơn Giá","Hình Ảnh"},null);                                               
                    Utility.ClearControlInPanel(null);
                    JOptionPane.showMessageDialog(this,"xóa thành công");                        
                } 
                catch(Exception ex){
                    JOptionPane.showMessageDialog(this, ex.getMessage());
                }                    
            }            
       }
       else{
           JOptionPane.showMessageDialog(this, "hãy chọn cửa hàng cần xóa bằng cách click lên cửa hàng dưới lưới");
       }
    }//GEN-LAST:event_btnXoaSPActionPerformed

    private void tbDanhSachSPMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachSPMouseClicked
        Object []temp =Utility.GetSelectedRows(tbDanhSachSanPham, sanphams);
        if(temp.length>0){
            SanPham sanPham =(SanPham)temp[0];
            SetDataSanPhamControls(sanPham);
      
             Utility.CreateColumnsFromDB(SanPham.class, sanphams, tbDanhSachSanPham,new String[]{"Mã San Phẩm","Sản Phẩm", "Tên Loại Sản Phẩm","Đơn Giá","Hình Ảnh"},null);                          
             //Utility.ClearControlInPanel(jPanel3);
             errSanPham.clear();
        }
    }//GEN-LAST:event_tbDanhSachSPMouseClicked

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        Object []temp =Utility.GetSelectedRows(tbDanhSachSanPham, sanphams);
        if(temp.length!=0){           
            try{
                int vt =sanphams.indexOf(temp[0]);                
                SanPham tenSP =sanphams.get(vt); 
                tenSP.setTenSanPham(txtTenSP.getText().trim());
                tenSP.setDonGia(Double.valueOf(txtDonGia.getText().trim()));
            //    tenSP.setHinhAnh(txtThemAnh.getText().trim());
               
                SanPhamDAO.SuaSanPham(tenSP);
                Utility.CreateColumnsFromDB(SanPham.class, sanphams, tbDanhSachSanPham,new String[]{"Mã San Phẩm","Sản Phẩm", "Tên Loại Sản Phẩm","Đơn Giá","Hình Ảnh"},null);                              
                JOptionPane.showMessageDialog(this, "Sửa thành công");
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }            
        }
    }//GEN-LAST:event_btnSuaActionPerformed

    private void tbDanhSachSanPhamMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachSanPhamMouseClicked
        Object[] temp = Utility.GetSelectedRows(tbDanhSachSanPham, sanphams);
        if (temp.length > 0) {
            SanPham sanPham = (SanPham) temp[0];
            SetDataSanPhamControls(sanPham);
            //  cuaHangs =new ArrayList<CuaHang>(String.valueOf(cuaHang.getId()));
            Utility.CreateColumnsFromDB(SanPham.class, sanphams, tbDanhSachSanPham, new String[]{"Mã San Phẩm","Sản Phẩm", "Tên Loại Sản Phẩm","Đơn Giá","Hình Ảnh"}, null);
            //Utility.ClearControlInPanel(jPanel3);
            errSanPham.clear();
        }

    }//GEN-LAST:event_tbDanhSachSanPhamMouseClicked

    private void btnTimSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimSPActionPerformed
        try {
            // TODO add your handling code here:
            
            Integer maSanPham = txtMaSP_Tim.getText().isEmpty() ? null : Integer.parseInt(txtMaSP_Tim.getText());
            Integer maLoaiSanPham =txtMaLSP_Tim.getText().isEmpty()? null: Integer.parseInt(txtMaLSP_Tim.getText());
            Double donGiaTu =txtDonGiaTu.getText().isEmpty()? null: Double.parseDouble(txtDonGiaTu.getText());
            Double donGiaDen =txtDonGiaDen.getText().isEmpty()? null: Double.parseDouble(txtDonGiaDen.getText());
            if(txtDonGiaTu.getText().isEmpty()==false && txtDonGiaDen.getText().isEmpty()==true){
                 JOptionPane.showMessageDialog(this, "hãy điền giá trị của Đơn giá đên");
                 return;
            }
            if(txtDonGiaTu.getText().isEmpty()==true && txtDonGiaDen.getText().isEmpty()==false){
                 JOptionPane.showMessageDialog(this, "hãy điền giá trị của Đơn giá từ");
                 return;
            }
            sanphams = SanPhamDAO.TimKiemSanPham(maLoaiSanPham, txtTenLSP_Tim.getText().trim(),maSanPham,txtTenSP_Tim.getText().trim(),donGiaTu,donGiaDen,false);            
            Utility.CreateColumnsFromDB(SanPham.class, sanphams, tbDanhSachSanPham, new String[]{"Mã San Phẩm","Sản Phẩm", "Tên Loại Sản Phẩm","Đơn Giá","Hình Ảnh"},null);
            if (sanphams.isEmpty()) {
                JOptionPane.showMessageDialog(this, "Không tìm thấy");
            }
        } catch (Exception ex) {
        //    logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, "từ khóa tìm kiếm không hợp lệ");
        }
    }//GEN-LAST:event_btnTimSPActionPerformed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        cmbMaLoaiSP.setSelectedIndex(-1);
        txtTenLoaiSP.setText("");
        txtMaSP.setText("");
        txtTenSP.setText("");
        txtMaLSP_Tim.setText("");
        txtDonGia.setText("");
       
        txtTenLSP_Tim.setText("");
        txtMaSP_Tim.setText("");
        txtTenSP_Tim.setText("");
        txtDonGiaTu.setText("");
        txtDonGiaDen.setText("");
        
    }//GEN-LAST:event_btnClearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnNhapSP;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThemLoaiSP;
    private javax.swing.JButton btnTimSP;
    private javax.swing.JButton btnXoaSP;
    private javax.swing.JComboBox cmbMaLoaiSP;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    public org.jdesktop.swingx.JXTable tbDanhSachSP;
    private org.jdesktop.swingx.JXTable tbDanhSachSanPham;
    private javax.swing.JTextField txtDonGia;
    private javax.swing.JTextField txtDonGiaDen;
    private javax.swing.JTextField txtDonGiaTu;
    private javax.swing.JTextField txtMaLSP_Tim;
    private javax.swing.JTextField txtMaSP;
    private javax.swing.JTextField txtMaSP_Tim;
    private javax.swing.JTextField txtTenLSP_Tim;
    private javax.swing.JTextField txtTenLoaiSP;
    private javax.swing.JTextField txtTenSP;
    private javax.swing.JTextField txtTenSP_Tim;
    // End of variables declaration//GEN-END:variables
}
